package com.bl.student.dao.redis;

import com.bl.student.dao.redis.model.UserInfo;
import com.google.common.base.Optional;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

/**
 * Created by yujingyi on 2017/5/12.
 */
@Service
@Log4j2
public class UserInfoDao extends AbstractDao {

    private final String USER_LOGIN_KEY_PREFIX = "LOGIN_";

    private final long USER_LOGIN_KEY_EXPIRE = 60 * 60 * 24 * 30;


    public Optional<UserInfo> getUserInfo(String mobile) {

        UserInfo authInfo = hgetAll(genKey(USER_LOGIN_KEY_PREFIX, mobile), UserInfo.class);

        return authInfo == null ?
                Optional.<UserInfo>absent() :
                Optional.fromNullable(authInfo);
    }

    public void setUserInfo(UserInfo authInfo) {
        String key = genKey(USER_LOGIN_KEY_PREFIX, authInfo.getMobile());
        hsetAll(key, authInfo);
        expire(key, USER_LOGIN_KEY_EXPIRE);
    }

    public void setUserInfoAttribute(String mobile, String key, String value) {

        hset(genKey(USER_LOGIN_KEY_PREFIX, mobile), key, value);

    }

    public boolean delUserInfo(String mobile) {

        return del(genKey(USER_LOGIN_KEY_PREFIX, mobile)) > 0 ? true : false;
    }


}
